package com.example.studentstatusmanagement.controller;

import com.example.studentstatusmanagement.config.SystemVariable;
import com.example.studentstatusmanagement.pojo.Major;
import com.example.studentstatusmanagement.pojo.MajorExample;
import com.example.studentstatusmanagement.pojo.Plot;
import com.example.studentstatusmanagement.pojo.PlotExample;
import com.example.studentstatusmanagement.pojo.doo.ReqClassroom;
import com.example.studentstatusmanagement.pojo.doo.ReqRoom;
import com.example.studentstatusmanagement.pojo.dto.SMajor;
import com.example.studentstatusmanagement.pojo.dto.SRoom;
import com.example.studentstatusmanagement.service.PlotService;
import com.example.studentstatusmanagement.utils.ResultResponse;
import com.example.studentstatusmanagement.utils.ResultTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("api/v1/plot")
@RestController
public class PlotController {


    @Autowired
    private PlotService plotService;

    @RequestMapping("showbuilding")
    public ResultResponse showBuilding() {
        return ResultResponse.Build(ResultTypeEnum.success, plotService.selectBuildingsForPlot());
    }

    @RequestMapping("show")
    public Map<String, Object> showMajorsInfo(ReqRoom reqRoom) {

        Map<String, Object> so = new HashMap<String, Object>();

        List<SRoom> ls = plotService.selectPlots(reqRoom);

        so.put("code", SystemVariable.URL_SUCCESS);
        so.put("msg", "");
        so.put("count", ls.size());
        so.put("data", ls);

        return so;
    }

    @RequestMapping("del")
    public ResultResponse del(Plot plot) {
        if (!StringUtils.isEmpty(plot.getId())) {
            plot.setIsdel(SystemVariable.IS_DEL);
            if (plotService.updateByPrimaryKeySelective(plot) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("use")
    public ResultResponse use(Plot plot) {
        if (!StringUtils.isEmpty(plot.getId())) {
            plot.setIsdel(SystemVariable.IS_NOT_DEL);
            if (plotService.updateByPrimaryKeySelective(plot) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("add")
    public ResultResponse add(Plot plot) {

        PlotExample example = new PlotExample();
        PlotExample.Criteria c = example.createCriteria();
        c.andBuildingIdEqualTo(plot.getBuildingId());
        c.andPlotnameEqualTo(plot.getPlotname());
        if (plotService.selectByExample(example).size() > 0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        if (plotService.insertSelective(plot) == SystemVariable.SUCCESS) {
            return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("update")
    public ResultResponse update(Plot plot) {
        PlotExample example = new PlotExample();
        PlotExample.Criteria c = example.createCriteria();
        c.andBuildingIdEqualTo(plot.getBuildingId());
        c.andPlotnameEqualTo(plot.getPlotname());
        c.andIdNotEqualTo(plot.getId());
        if (plotService.selectByExample(example).size() > 0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        if (!StringUtils.isEmpty(plot.getId())) {
            if (plotService.updateByPrimaryKeySelective(plot) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
}
